package com.woniuxy.redis01;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniuxy.redis01.entity.Student;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.*;

import java.util.Set;

@SpringBootTest
class Redis01ApplicationTests02 {

    //SpringBoot封装的操作Redis的核心类型
    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Test
    void test04() {
        //通用指令redisTemplate
        //keys *
        System.out.println(redisTemplate.keys("*"));
        //exists
        System.out.println(redisTemplate.hasKey("a"));
        System.out.println(redisTemplate.hasKey("b"));
        System.out.println(redisTemplate.countExistingKeys(redisTemplate.keys("*")));
    }

    @Test
    void test05() {
        ListOperations opsForList = redisTemplate.opsForList();
//        opsForList.rightPushAll("list1", "a","b","c");
        //lrange
//        System.out.println(opsForList.range("list1", 0, -1));
        String str;
        //rpop
        while((str= (String) opsForList.rightPop("list1"))!=null){
            System.out.println(str);
        }

    }
    @Test
    void test06() {
        ZSetOperations opsForZSet = redisTemplate.opsForZSet();
        //zadd
//        opsForZSet.add("z1", "zs",90);
//        opsForZSet.add("z1", "ls",95);
//        opsForZSet.add("z1", "ww",80);
//
//        Set z1 = opsForZSet.range("z1", 0, -1);
//        System.out.println(z1);

        Set<ZSetOperations.TypedTuple<String>> z1 = opsForZSet.rangeByScoreWithScores("z1", 0, 100);
        for(ZSetOperations.TypedTuple<String> obj : z1){
            System.out.println(obj.getScore());
            System.out.println(obj.getValue());
        }
    }

    @Test
    void contextLoads() {
        //RedisTemplate提过了针对不同数据类型操作的方法
        ValueOperations opsForValue = redisTemplate.opsForValue();
        HashOperations opsForHash = redisTemplate.opsForHash();
        ListOperations opsForList = redisTemplate.opsForList();
        SetOperations opsForSet = redisTemplate.opsForSet();
        ZSetOperations opsForZSet = redisTemplate.opsForZSet();
    }

    @Test
    void test01() {
        //针对String类型的操作
        ValueOperations opsForValue = redisTemplate.opsForValue();
        opsForValue.set("b", "bbb");
        System.out.println(opsForValue.get("b"));
        //K key, V value, long timeout, TimeUnit unit
//        opsForValue.set("b", "bbb", 30, TimeUnit.SECONDS);
//        System.out.println(opsForValue.setIfAbsent("b", "b"));
//        System.out.println(opsForValue.setIfAbsent("c", "c"));
//        System.out.println(opsForValue.setIfAbsent("c", "c"));
    }

    @Test
    void test02() throws JsonProcessingException {
        ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();
        opsForValue.set("b", "bbbaaa");
        System.out.println(opsForValue.get("b"));
        Student student=new Student(1, "zs",11);
        opsForValue.set("s1",new ObjectMapper().writeValueAsString(student));
        String strStudent = opsForValue.get("s1");
        Student student1 = new ObjectMapper().readValue(strStudent, Student.class);
        System.out.println(student1);
    }
    @Test
    void test03() {
        ValueOperations opsForValue = redisTemplate.opsForValue();
        Student student=new Student(2, "ls",12);
        opsForValue.set("s2", student);
        System.out.println(opsForValue.get("s2"));
        opsForValue.set("a", "aaa");
        System.out.println(opsForValue.get("a"));
    }

}
